<template>
  <div class="app-container">
    <el-tree :data="treeData" :props="treeProps" default-expand-all :expand-on-click-node="false"
             style="display: inline-block" @node-click="handleNodeClick">
        <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span style="margin-left: 20px">
          <el-button v-if="node.level != 3" type="text" size="mini" @click="() => handleAdd(node, data)">添加</el-button>
          <el-button v-if="node.level != 1" type="text" size="mini" @click="() => handleUpdate(node, data)">修改</el-button>
          <el-button v-if="node.level != 1" type="text" size="mini" @click="() => handleDelete(node, data)">删除</el-button>
        </span>
      </span>
    </el-tree>

    <!-- 添加或修改材料类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级" prop="materiatypePid">
          <el-input v-model="form.parentName" disabled/>
        </el-form-item>
        <el-form-item label="类型名称" prop="materiatypeName">
          <el-input v-model="form.materiatypeName" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMaterialTypeTree,
  listMaterialType,
  getMaterialType,
  delMaterialType,
  addMaterialType,
  updateMaterialType,
} from "@/api/material/materialType";

export default {
  name: "MaterialType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料类型表格数据
      materialTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentName:null,
        materiatypeName: null,
        materiatypePid: null,
        level: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      treeData: [],
      treeProps: {
        label: 'name',
        children: 'children'
      },
    };
  },
  created() {
    this.getTree();
  },
  /** 查询材料类型列表 */
  getList() {
    this.loading = true;
    listMaterialType(this.queryParams).then(response => {
      this.materialTypeList = response.rows;
      this.total = response.total;
      this.loading = false;
    });
  },
  methods: {
    handleNodeClick(data){
      console.log(data);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materiatypeId: null,
        materiatypeName: null,
        materiatypePid: null,
        level: null
      };
      this.resetForm("form");
    },
    getTree(){
      getMaterialTypeTree()
        .then((resp)=>{
          this.treeData=[resp.data];
        }).catch(()=>{})
    },
    /** 新增 */
    handleAdd(node,data) {
      console.log('a',data)
      this.reset();
      this.open = true;
      this.title = "添加材料类型";
      this.form.parentName = node.label;
      this.form.level = node.level;
      this.form.materiatypePid = data.id;
    },
    /** 修改 */
    handleUpdate(node,data) {
      this.title = "修改材料类型";
      this.open = true;
      this.reset();
      getMaterialType(data.id)
        .then((resp) => {
          this.form = resp.data
          this.form.parentName = node.parent.label
        }).catch(() => {
      })
    },
    /** 提交按钮 */
    submitForm(form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          if (this.form.materiatypeId != null) {
            updateMaterialType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getTree()
            });
          } else {
            if (this.form.level === 4) {
              this.$modal.alertWarning("不能添加4级材料类型");
              return;
            }
            addMaterialType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getTree()
            });
          }
        }
      });
    },
    /** 删除 */
    handleDelete(node, data) {
      this.$modal.confirm('确定要删除吗？')
        .then(()=>{
          delMaterialType(data.id)
            .then(response => {
              this.$modal.msgSuccess("删除成功");
              this.getTree()
            })
        }).catch(()=>{})
    },
  }
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
